package com.wky.system.controller;

import com.wky.basic.Result.JsonResult;
import com.wky.basic.Result.PageResult;
import com.wky.system.pojo.SystemDetail;
import com.wky.system.service.SystemDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 数据字典明细
 */

@RestController
@RequestMapping("/system/detail")
@Tag(name = "数据字典明细")
@CrossOrigin
public class SystemDetailController {

    @Autowired
    private SystemDetailService systemDetailService;

    /*
    * 查询所有部门
     */
    @Operation(method ="查询所有部门")
    @GetMapping("/findAll")
    public JsonResult findAll(){
        try {
            return JsonResult.success(systemDetailService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  根据id查询部门
     */
    @GetMapping("/findById")
    public JsonResult findById(Long id){
        try {
            return JsonResult.success(systemDetailService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  分页查询部门
     */
    @PostMapping("/findByPage")
    public JsonResult findByPage(@RequestBody SystemDetail systemDetail){
        try {

            return JsonResult.success(systemDetailService.findByPage(systemDetail));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  添加部门
     */
    @PostMapping("/add")
    public JsonResult add(@RequestBody SystemDetail systemDetail){
        try {
//            if (systemDetail.getTypesId() == null){
//                return JsonResult.error("品牌id不能为空");
//            }
            systemDetailService.add(systemDetail);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }

    }

    /**
     *  修改部门
     */
    @PostMapping("/update")
    public JsonResult update(@RequestBody SystemDetail systemDetail){
        try {
            systemDetailService.update(systemDetail);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  删除部门
     */
    @DeleteMapping("/delete")
    public JsonResult delete(Long id){
        try {
            if (id == null){
                return JsonResult.error("字典明细id不能为空");
            }
            systemDetailService.delete(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    /**
     *  批量删除部门
     */
    @DeleteMapping("/deleteByIds")
    public JsonResult deleteBatch(@RequestBody Long[] ids){
        for (Long id : ids) {
            systemDetailService.delete(id);
        }
        return JsonResult.success();
    }


}
